package com.bootdo.common.utils;


import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * Created by ramon on 2017/7/24.
 */
public class DateUtil {
    /**
     * 一天的毫秒数
     */
    public static final long DAY_MILLI_SECONDS = 1000*3600*24L;
    public static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter dateWithHourFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
    public static ZoneOffset zoneOffset = OffsetDateTime.now().getOffset();

    /**
     * yyyy-MM-dd
     */
    public static Long parseDateTimeMin(String dateStr) {
        return parseDate(dateStr,LocalTime.MIN);
    }

    public static Long parseDateTimeMax(String dateStr) {
        return parseDate(dateStr,LocalTime.MAX);
    }

    public static Long parseDate(String dateStr,LocalTime localTime) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.parse(dateStr, dateFormatter), localTime);
        return localDateTime.toInstant(zoneOffset).toEpochMilli();
    }

    public static String getCurrentDateStr() {
        LocalDate localDate = LocalDate.now();
        return localDate.format(dateFormatter);
    }

    public static String getCurrentShortDateStr() {
        LocalDate localDate = LocalDate.now();
        return localDate.format(shortDateFormatter);
    }

    public static String getYesterdayDateStr() {
        LocalDate localDate = LocalDate.now().minusDays(1);
        return localDate.format(dateFormatter);
    }

    public static String getYesterdayShortDateStr() {
        LocalDate localDate = LocalDate.now().minusDays(1);
        return localDate.format(shortDateFormatter);
    }

    /**
     * 从当前时间往后推days天的时间
     * @param days
     * @return
     */
    public static Long getDateAfterDays(Long days){
        LocalDateTime localDate = LocalDateTime.now().plusDays(days);
        return localDate.toInstant(zoneOffset).toEpochMilli();
    }

    /**
     * yyyy-MM-dd
     */
    public static String getDateStr(Long time) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(time / 1000, 0, zoneOffset);
        return localDateTime.format(dateFormatter);
    }

    /**
     * yyyyMMdd
     */
    public static String getShortDateStr(Long time) {
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(time / 1000, 0, zoneOffset);
        return localDateTime.format(shortDateFormatter);
    }

    public static String getDateStrByFormatter(Long time,DateTimeFormatter dateFormatter){
        return dateFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
    }

    public static String getDateWithHour(LocalDateTime dateTime) {
        return dateTime.format(dateWithHourFormatter);
    }

    /**
     * 计算目标日期距离当前日期的天数差值
     * @param target
     * @return
     */
    public static long getDifferDays(long target){
        long seconds = System.currentTimeMillis();
        return (target - seconds)/DAY_MILLI_SECONDS + 1;
    }
}
